package com.simpletour.qa.common.base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

/**
 * 通用biz
 * @author zhaohw
 * @date 2018-06-20 11:29
 */
public abstract class BaseBiz<JPA extends JpaRepository<Entity,ID>,Entity,ID>{

    @Autowired
    private JPA jpaRepository;

    public void setJpaRepository(JPA jpa){
        this.jpaRepository = jpa;
    }
    /**
     * 通过id查找
     * @param id
     * @return
     */
    public Optional<Entity> findById(ID id) {
        return jpaRepository.findById(id);
    }

    /**
     * 查找所有
     * @return
     */
    public List<Entity> findAll(){
        return jpaRepository.findAll();
    }

    /**
     * 保存
     * @param entity
     * @return
     */
    @Transactional(rollbackOn = Exception.class)
    public Entity save(Entity entity){
        return jpaRepository.save(entity);
    }


    /**
     * 通过id删除
     * @param id
     */
    @Transactional(rollbackOn = Exception.class)
    public void deleteById(ID id){
        jpaRepository.deleteById(id);
    }

}
